from django.contrib.auth.hashers import check_password
from app01.models import User


# authenticate django自带的认证
class EmailAuthBacked:

    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(email=username)
        except User.DoesNotExist:
            return None
        if user and check_password(password, user.password):
            return user
        else:
            return None


class PhoneAuthBacked:

    def authenticate(self, request, username=None, password=None):
        try:
            user = User.objects.get(phone=username)
        except User.DoesNotExist:
            return None
        if user and check_password(password, user.password):
            return user
        else:
            return None
